package com.hctg.forced2sleep.activity;

import android.Manifest;
import android.app.KeyguardManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.actor.myandroidframework.utils.LogUtils;
import com.actor.myandroidframework.utils.toaster.ToasterUtils;
import com.github.dubu.lockscreenusingservice.Lockscreen;
import com.hctg.forced2sleep.databinding.ActivityLockUnLockScreenBinding;
import com.hctg.forced2sleep.service.MyAccessibilityService;
import com.hctg.forced2sleep.utils.KeyguardManagerUtils;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.List;

/**
 * description: 屏幕锁屏/解锁
 * company    :
 * @author    : ldf
 * date       : 2024/5/20 on 16:08
 */
public class LockUnLockScreenActivity extends BaseActivity<ActivityLockUnLockScreenBinding> {

    private View lockScreenView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding.btnView.setOnClickListener(v -> {
            getInfo();
        });
        viewBinding.btnLock.setOnClickListener(v -> {
//            disableKeyguard();
//            lockScreenView = FloatViewUtils.showLockScreenView();
        });
        viewBinding.btnLockCancel.setOnClickListener(v -> {
//            FloatViewUtils.removeView(lockScreenView);
//            lockScreenView = null;
        });
    }

    private void getInfo() {
        boolean keyguardLocked = KeyguardManagerUtils.isKeyguardLocked();
        boolean keyguardSecure = KeyguardManagerUtils.isKeyguardSecure();
        LogUtils.errorFormat("keyguardLocked=%b, keyguardSecure=%b", keyguardLocked, keyguardSecure);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 无障碍服务启用状态更新
        boolean isLaunch = MyAccessibilityService.get() != null;
        setTitle("无障碍服务是否已启用: " + isLaunch);
    }

    private void disableKeyguard() {
        if (true) {
            if (XXPermissions.isGranted(this, Permission.SYSTEM_ALERT_WINDOW)) {
                Lockscreen.getInstance(this).startLockscreenService();
            } else {
                XXPermissions.with(this)
                        .permission(Permission.SYSTEM_ALERT_WINDOW)
                        .request(new OnPermissionCallback() {
                            @Override
                            public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                                disableKeyguard();
                            }
                            @Override
                            public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                                OnPermissionCallback.super.onDenied(permissions, doNotAskAgain);
                                ToasterUtils.warning("请同意权限!");
                                XXPermissions.startPermissionActivity(mActivity, Permission.SYSTEM_ALERT_WINDOW);
                            }
                        });
            }
            return;
        }

        if (true) {
            MyAccessibilityService myAccessibilityService = MyAccessibilityService.get();
            if (myAccessibilityService != null) {
                myAccessibilityService.actionLockScreen();
            }
            return;
        }

        if (XXPermissions.isGranted(this, Manifest.permission.DISABLE_KEYGUARD, Manifest.permission.WAKE_LOCK)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                KeyguardManagerUtils.disableKeyguard(this, new KeyguardManager.KeyguardDismissCallback() {
                    @Override
                    public void onDismissError() {
                        super.onDismissError();
                        LogUtils.error("onDismissError");
                    }

                    @Override
                    public void onDismissSucceeded() {
                        super.onDismissSucceeded();
                        LogUtils.error("onDismissSucceeded");
                    }

                    @Override
                    public void onDismissCancelled() {
                        super.onDismissCancelled();
                        LogUtils.error("onDismissCancelled");
                    }
                });
            }
        } else {
            XXPermissions.with(this)
                    .permission(Manifest.permission.DISABLE_KEYGUARD, Manifest.permission.WAKE_LOCK)
                    .request(new OnPermissionCallback() {
                        @Override
                        public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                            disableKeyguard();
                        }
                        @Override
                        public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                            OnPermissionCallback.super.onDenied(permissions, doNotAskAgain);
                            ToasterUtils.warning("请同意权限!");
                            XXPermissions.startPermissionActivity(mActivity, Manifest.permission.DISABLE_KEYGUARD, Manifest.permission.WAKE_LOCK);
                        }
                    });
        }
    }
}